#include<bits/stdc++.h>
using namespace std;
int num[200005];
int mark[200005];
bool col[200005];
int T, n;
int maxn = -1;
int ln = -1;
int tot;
vector<int>b;
void dfs(int x, int c_){
	col[x] = 0;s
	int c = 0;
	for(int i = x - 1; i >= 0; i--)	if(col[i] == 0){if(b[i] == b[x]) c = b[i];break;}
	if(x < tot)	dfs(x + 1, c_ + c);
	col[x] = 1;
	int c2 = 0;
	for(int i = x - 1; i >= 0; i--)	if(col[i] == 1){if(b[i] == b[x]) c2 = b[i];break;}
	if(x < tot)	dfs(x + 1, c_ + c2);
	if(x == tot)	maxn = max(maxn, c_ + max(c, c2));
}
int main(){
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	scanf("%d", &T);
	while(T--){
		tot = -1;
		memset(mark, 0, sizeof mark);
		maxn =-1; 
		ln = -1;
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			scanf("%d", num + i);
			mark[num[i]]++;
		}
		bool flag=1;
		for(int i = 0; i < n; i++){
			if(mark[num[i]] > 1)	++tot, b.push_back(num[i]), flag = 1;
			else if(flag)	++tot, b.push_back(0), flag = 0;
		}
		if(tot == -1){
			puts("0");
			continue;
		}
		dfs(0, 0);
		printf("%d\n", maxn);
	}
}